/*   
 * Copyright (c) 2010-2020 DMGIS Ltd. All Rights Reserved.   
 *   
 * This software is the confidential and proprietary information of   
 * DMGIS. You shall not disclose such Confidential Information   
 * and shall use it only in accordance with the terms of the agreements   
 * you entered into with DMGIS.   
 *   
 */
package com.dmgis.qxfw.dataservice.businessservice.yynl.service.Impl;


import com.dmgis.qxfw.dataservice.businessservice.api.domain.DbConfig;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.yj.warn_signal_info;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.zy.*;
import com.dmgis.qxfw.dataservice.businessservice.common.domain.LogQuery;
import com.dmgis.qxfw.dataservice.businessservice.common.entity.LogWarning;
import com.dmgis.qxfw.dataservice.businessservice.common.entity.SysConfig;
import com.dmgis.qxfw.dataservice.businessservice.common.entity.WaterLogPoint;
import com.dmgis.qxfw.dataservice.businessservice.common.entity.WaterLoggingSet;
import com.dmgis.qxfw.dataservice.businessservice.common.util.DateTime;
import com.dmgis.qxfw.dataservice.businessservice.common.util.JsonUtil;
import com.dmgis.qxfw.dataservice.businessservice.common.util.StringUtil;
import com.dmgis.qxfw.dataservice.businessservice.controller.ProcessRequestController;
import com.dmgis.qxfw.dataservice.businessservice.domain.RequestDataBySelect;
import com.dmgis.qxfw.dataservice.businessservice.yynl.service.IWarnQueryService;
import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("LogService")
public class WarnQueryService implements IWarnQueryService {
	ProcessRequestController pc = new ProcessRequestController();
	@Override
	public Date getLogNewDate() {
		// TODO 获取内涝预警最新时间
		waterlogging_fcst newDate = new waterlogging_fcst();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		String[] columns = new String[] { "forecasttime" };
		selectRequest.setColumns(columns);
		selectRequest.setSort(new String[] { "forecasttime desc" });
		selectRequest.setLimit(1);
		try {
			selectParam.put("method", "waterlogging_fcst");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = pc.processRequest(selectParam);
			newDate = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<waterlogging_fcst>>() {
			}).get(0);
		} catch (Exception e) {
			// TODO: handle exception
			newDate.setForecasttime(DateTime.now().toDate());
		}
		return newDate.getForecasttime();
	}

	@Override
	public List<LogWarning> getLogInfo(LogQuery logQuery) {
		// TODO 获取内涝预警信息
		List<waterlogging_fcst> listData = new ArrayList<waterlogging_fcst>();
		List<LogWarning> resultList = new ArrayList<LogWarning>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		StringBuilder strWhere = new StringBuilder();
		int whereCount = 0;
		if (logQuery.getStartDate() != null) {
			//whereCount++;
			strWhere.append(String.format("and forecasttime>='%s'", logQuery.getStartDate()));
		}
		if (logQuery.getEndDate() != null) {
			strWhere.append(String.format(" and forecasttime<='%s'", logQuery.getEndDate()));
		}
		if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
			strWhere = strWhere.replace(0, 4, "");
		}
		selectRequest.setParam(strWhere.toString());
		selectRequest.setSort(new String[]{"forecasttime desc"});
		try {
			selectParam.put("method", "waterlogging_fcst");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = pc.processRequest(selectParam);
			listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<waterlogging_fcst>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		if (listData != null && listData.size() > 0) {
			for (waterlogging_fcst water : listData) {
				LogWarning logWarning = new LogWarning();
				logWarning.setId(water.getId());
				logWarning.setForecastTime(water.getForecasttime());
				logWarning.setRainType(water.getRaintype());
				logWarning.setWarnArea(water.getWarnarea());
				logWarning.setWarnPos(water.getWarnpos());
				logWarning.setDocPath(water.getDocpath());
				resultList.add(logWarning);
			}
		}
		return resultList;
	}

	@Override
	public Map<Integer, WaterLoggingSet> getLogLevelInfo() {
		// TODO 预警颜色说明
		// 内涝点预报
		List<waterlogging_levelsetting> listData = new ArrayList<waterlogging_levelsetting>();
		// 内涝点信息
		Map<Integer, WaterLoggingSet> resultList = new HashMap<Integer, WaterLoggingSet>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		try {
			selectParam.put("method", "waterlogging_levelsetting");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = pc.processRequest(selectParam);
			listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<waterlogging_levelsetting>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
		}
		if (listData != null && listData.size() > 0) {
			for (waterlogging_levelsetting pfcst : listData) {
				WaterLoggingSet logSet = new WaterLoggingSet();
				logSet.setColor(pfcst.getColor());
				logSet.setGradeType(pfcst.getGradetype());
				resultList.put(logSet.getColor(), logSet);
			}
		}
		return resultList;
	}

	@Override
	public List<WaterLogPoint> getLogPointInfo(int docId) {
		// TODO 内涝点信息
		List<waterlogging_pointfcst> listData1 = new ArrayList<waterlogging_pointfcst>();
		// 内涝点信息
		List<waterlogging_pointinfo> listData2 = new ArrayList<waterlogging_pointinfo>();
		List<WaterLogPoint> resultList = new ArrayList<WaterLogPoint>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		selectRequest.setParam(String.format("warningid='%d'", docId));
		try {
			selectParam.put("method", "waterlogging_pointfcst");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = pc.processRequest(selectParam);
			listData1 = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<waterlogging_pointfcst>>() {
			});
			selectParam.clear();
			selectRequest = new RequestDataBySelect();
			selectParam.put("method", "waterlogging_pointinfo");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			message = pc.processRequest(selectParam);
			listData2 = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<waterlogging_pointinfo>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
		}
		if (listData1 != null && listData1.size() > 0) {
			for (waterlogging_pointfcst pfcst : listData1) {
				WaterLogPoint logSet = new WaterLogPoint();
				for (waterlogging_pointinfo pInfo : listData2) {
					if (Integer.parseInt(pfcst.getWaterloggingpointid()) == pInfo.getId()) {
						logSet.setLogId(pInfo.getId());
						logSet.setWarnLevel(pfcst.getWaterlogginglevel());
						if (pfcst.getWarningwaterdepth() != null) {
							logSet.setDepth(pfcst.getWarningwaterdepth());
						}
						if (pfcst.getRealwaterdepth() != null) {
							logSet.setSkDepth(pfcst.getRealwaterdepth());
						}
						logSet.setLogName(pInfo.getName());
						logSet.setCounty(pInfo.getCounty());
						logSet.setLon(pInfo.getLon());
						logSet.setLat(pInfo.getLat());
						logSet.setTel(pInfo.getTel());
						logSet.setAddress(pInfo.getDetailposition());
						break;
					}
				}
				resultList.add(logSet);
			}
		}
		return resultList;
	}

	@Override
	public List<warn_signal_info> getWarningInfo(String startDate, String endDate, int id) {
		// TODO 预警信号表
		List<warn_signal_info> resultList = new ArrayList<warn_signal_info>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		List<String> columns = new ArrayList<String>();
		columns.add("id");
		columns.add("Area");
		columns.add("Type");
		columns.add("Color");
		columns.add("City");
		columns.add("AnnouncedTime");
		columns.add("County");
		columns.add("Standard");
		columns.add("DefenseGuide");
		columns.add("bFree");
		selectRequest.setColumns(columns.toArray(new String[columns.size()]));
		StringBuilder strWhere = new StringBuilder();
		int whereCount = 0;
		if (!StringUtil.IsNullOrEmpty(startDate)) {
			whereCount++;
			strWhere.append(String.format("AnnouncedTime>='%s'", startDate));
		}
		if (!StringUtil.IsNullOrEmpty(endDate)) {
			strWhere.append(String.format(" and AnnouncedTime<='%s'", endDate));
		}
		if (id > 0) {
			strWhere.append(String.format(" and id=%d", id));
		}
		strWhere.append(" and bfree=0");
		if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
			strWhere = strWhere.replace(0, 4, "");
		}
		selectRequest.setParam(strWhere.toString());
		selectRequest.setSort(new String[]{"AnnouncedTime desc"});
		try {
			selectParam.put("method", "warn_signal_info");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = pc.processRequest(selectParam);
			resultList = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<warn_signal_info>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
		}
		return resultList;
	}

	@Override
	public String getProductID(String docPath) {
		// TODO Auto-generated method stub
		List<base_product> listData = new ArrayList<base_product>();
		String result="";
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		StringBuilder strWhere = new StringBuilder();
		if (!StringUtil.IsNullOrEmpty(docPath)) {
			strWhere.append(String.format("productname='%s'", docPath));
		}
		selectRequest.setParam(strWhere.toString());
		try {
			selectParam.put("method", "base_product");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = pc.processRequest(selectParam);
			listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<base_product>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
		}
		if (listData != null && listData.size() > 0) {
			for (base_product dzWarn : listData) {
				result = dzWarn.getId().toString();
			}
		}
		return result;
	}

	@Override
	public List<waterlogging_pointinfo> getwaterlogpnt() {
		// TODO 易涝点
		List<waterlogging_pointinfo> listData1 = new ArrayList<waterlogging_pointinfo>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		selectRequest.setParam(String.format(" 1=1 and detailposition like '%s%%'", DbConfig.CITY));
		try {
			selectParam.put("method", "waterlogging_pointinfo");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = pc.processRequest(selectParam);
			listData1 = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<waterlogging_pointinfo>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
		}
		return listData1;
	}

	@Override
	public List<waterlogging_fcst> getNLWarning() {
		List<waterlogging_fcst> listData = new ArrayList<waterlogging_fcst>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		selectRequest.setSort(new String[]{"forecasttime desc"});
		selectRequest.setLimit(1);
		try {
			selectParam.put("method", "waterlogging_fcst");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = pc.processRequest(selectParam);
			listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<waterlogging_fcst>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
		}
		return listData;
	}



	@Override
	public Date InitNltime() {
		base_product newDate = new base_product();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		String[] columns = new String[]{"to_char(maketime, 'yyyy-MM-dd HH24:MI:SS') as maketime"};
		selectRequest.setColumns(columns);
		selectRequest.setParam("productType='城市内涝短时预报'");
		selectRequest.setSort(new String[]{"maketime desc"});
		selectRequest.setLimit(1);
		try {
			selectParam.put("method", "base_product");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = pc.processRequest(selectParam);
			newDate = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<base_product>>() {
			}).get(0);
		} catch (Exception e) {
			// TODO: handle exception
			newDate.setMaketime(DateTime.now().toDate());
		}
		return newDate.getMaketime();
	}

	@Override
	public List<base_product> showHourImg(String startDate, String endDate) {
		List<base_product> base_products = new ArrayList<base_product>();
		base_product newDate = new base_product();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		// 拼接where字段
		StringBuilder strWhere = new StringBuilder();
		if (startDate != null) {
			strWhere.append(String.format("maketime>='%s'",startDate));
		}
		if (endDate != null) {
			strWhere.append(String.format(" and maketime<='%s'",endDate));
		}
		strWhere.append(" and productType='城市内涝短时预报'");
		selectRequest.setParam(strWhere.toString());
		selectRequest.setSort(new String[]{"maketime asc"});
		try {
			selectParam.put("method", "base_product");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = pc.processRequest(selectParam);
			base_products = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<base_product>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
			newDate.setMaketime(DateTime.now().toDate());
		}
		return base_products;
	}

	@Override
	public List<SysConfig> GetCatchmentarea() {
		List<SysConfig> sys = new ArrayList<SysConfig>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		selectRequest.setParam("paramtype='汇水区'");
		try {
			selectParam.put("method", "sys_config");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = pc.processRequest(selectParam);
			sys = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<SysConfig>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
		}
		return sys;



	}


}
